package com.stack.auto_parts.common;

import com.stack.auto_parts.common.enums.CommonErrorEnum;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.http.HttpStatus;

/**
 * @Title: Result
 * @Author WUYU_MIN
 * @Package com.stack.auto_parts.common
 * @Date 2024/7/8 18:30
 * @description:
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ApiResult<T> {

    private Integer code;

    private String message;

    private boolean success;

    private T data;

    public static <T> ApiResult<T> success() {
        ApiResult<T> apiResult = new ApiResult<T>();
        apiResult.setCode(HttpStatus.OK.value());
        apiResult.setSuccess(Boolean.TRUE);
        apiResult.setData(null);
        return apiResult;
    }
    public static <T> ApiResult<T> success(String message) {
        ApiResult<T> apiResult = new ApiResult<T>();
        apiResult.setCode(0);
        apiResult.setMessage(message);
        apiResult.setSuccess(Boolean.TRUE);
        apiResult.setData(null);
        return apiResult;
    }
    public static <T> ApiResult<T> success(T data) {
        ApiResult<T> apiResult = new ApiResult<T>();
        apiResult.setData(data);
        apiResult.setSuccess(Boolean.TRUE);
        return apiResult;
    }

    public static <T> ApiResult<T> fail(String message) {
        ApiResult<T> apiResult = new ApiResult<T>();
        apiResult.setCode(CommonErrorEnum.BUSINESS_ERROR.getCode());
        apiResult.setMessage(message);
        apiResult.setSuccess(Boolean.FALSE);
        apiResult.setData(null);
        return apiResult;
    }
}
